/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wuhenzhizao.ohostitlebar.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.service.WindowManager;
import org.wuhenzhizao.ohostitlebar.ResourceTable;
import org.wuhenzhizao.ohostitlebar.adapter.ListItemProvider;
import org.wuhenzhizao.ohostitlebar.beans.ListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * MainAbilitySlice
 *
 * @since 2021-06-08
 */
public class MainAbilitySlice extends AbilitySlice {
    private ListContainer mListContainer;
    private DependentLayout mDependentLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer);
        mDependentLayout = (DependentLayout) findComponentById(ResourceTable.Id_Main_dep);

        List<ListBean> list = getData();
        ListItemProvider mListItemProvider = new ListItemProvider(list, this);
        mListContainer.setItemProvider(mListItemProvider);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        initViews();
        initListeners();
    }

    private void initViews() {
        ShapeElement element = initChildShapeElement();
        element.setShape(ShapeElement.RECTANGLE);
        RgbColor[] rgbColors = new RgbColor[]{new RgbColor(38, 196, 253), new RgbColor(56, 124, 253)};
        element.setRgbColors(rgbColors);
        element.setGradientOrientation(ShapeElement.Orientation.BOTTOM_END_TO_TOP_START);
        mDependentLayout.setBackground(element);
    }

    /**
     * 初始化子组件背景
     *
     * @return ShapeElement
     */
    private ShapeElement initChildShapeElement() {
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(34, 197, 255));
        return element;
    }

    private void initListeners() {
        mListContainer.setLongClickable(false);
        mListContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                Intent mIntent = new Intent();
                Operation operation;
                if (i == 0) {
                    operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName("org.wuhenzhizao.ohostitlebar")
                            .withAbilityName("org.wuhenzhizao.ohostitlebar.ListAbility")
                            .build();
                    mIntent.setParam("xcf", "xcf");
                } else {
                    operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName("org.wuhenzhizao.ohostitlebar")
                            .withAbilityName("org.wuhenzhizao.ohostitlebar.TopAbility")
                            .build();
                    mIntent.setParam("position", i);
                }
                mIntent.setOperation(operation);
                startAbility(mIntent);
            }
        });
    }

    private ArrayList<ListBean> getData() {
        ArrayList<ListBean> list = new ArrayList<>();
        list.add(new ListBean("快速预览"));
        list.add(new ListBean("1、左边TextView + 中间文字"));
        list.add(new ListBean("2、左边ImageButton + 中间文字(带进度条)"));
        list.add(new ListBean("3、左边自定义Layout + 中间文字"));
        list.add(new ListBean("4、中间文字 + 右边TextView"));
        list.add(new ListBean("5、中间文字 + 右边ImageButton"));
        list.add(new ListBean("6、中间文字 + 右边自定义Layout"));
        list.add(new ListBean("7、中间跑马灯效果 + 右边TextView"));
        list.add(new ListBean("8、中间添加副标题"));
        list.add(new ListBean("9、中间自定义Layout + 右边自定义Layout"));
        list.add(new ListBean("10、中间搜索框"));
        list.add(new ListBean("11、中间搜索框 + 两侧自定义Layout"));
        return list;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
